package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.autodata.hotpotjump.*;
import com.yanqu.road.logic.activity.hotpotjump.game.HotpotJumpShowGridChange;
import com.yanqu.road.pb.activity.HotpotJumpProto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HotpotJumpPb {
    public static HotpotJumpProto.HotpotJumpUserDataSyncMsg.Builder parseHotpotJumpUserDataSyncMsg(UserHotpotJumpData data, UserHotpotJumpGameData gameData) {
        HotpotJumpProto.HotpotJumpUserDataSyncMsg.Builder builder = HotpotJumpProto.HotpotJumpUserDataSyncMsg.newBuilder();
        builder.setActivityId(data.getActivityId());
        builder.setScore(data.getScore());
        builder.setGameRound(gameData.getRound());
        builder.setGameTimes(data.getGameTimes());
        if (gameData.getRound() > 0) {
            builder.setIsPractice(gameData.isPractice());
        }
        builder.setGuide(data.getGuide());
        builder.setMaxRound(data.getMaxRound());
        return builder;
    }

    public static HotpotJumpProto.HotpotJumpGameTemp.Builder parseGameDataBuilder(UserHotpotJumpGameData userGameData) {
        HotpotJumpProto.HotpotJumpGameTemp.Builder builder = HotpotJumpProto.HotpotJumpGameTemp.newBuilder();
        builder.setRound(userGameData.getRound());
        builder.setScore(userGameData.getScore());
        builder.setPotId(userGameData.getPotId());
        builder.setMainRole(parseMainRoleBuilder(userGameData.getMainRole()));
        for (HotpotJumpUnit unit : userGameData.getUnits()) {
            builder.addUnits(parseUnitBuilder(unit));
        }
        for (HotpotJumpGrid effectGrid : userGameData.getEffectGrids()) {
            builder.addEffectGrids(parseGridBuilder(effectGrid));
        }
        builder.addAllSkillIds(userGameData.getSkillIds());
        builder.setReviveTimes(userGameData.getReviveTimes());
        builder.setRefreshTimes(userGameData.getRefreshTimes());
        builder.setIsPractice(userGameData.isPractice());
        builder.setStep(userGameData.getGameStep());
        builder.setJumpAmount(userGameData.getJumpAmount());

        return builder;
    }

    public static HotpotJumpProto.HotpotJumpUnitsTemp parseUnitBuilder(HotpotJumpUnit unit) {
        HotpotJumpProto.HotpotJumpUnitsTemp.Builder builder = HotpotJumpProto.HotpotJumpUnitsTemp.newBuilder();
        builder.setId(unit.getId());
        builder.setConfigId(unit.getConfigId());
        builder.setLevel(unit.getLevel());
        builder.setRewardExp(unit.getExp());
        builder.setHeadPos(parseGridBuilder(unit.getHeadPos()));
        for (HotpotJumpGrid grid : unit.getBodyPos()) {
            builder.addBodyPos(parseGridBuilder(grid));
        }
        builder.setAtk(unit.getAtk());
        return builder.build();
    }

    public static HotpotJumpProto.HotpotMainRoleTemp.Builder parseMainRoleBuilder(HotpotJumpMainRole mainRole) {
        HotpotJumpProto.HotpotMainRoleTemp.Builder builder = HotpotJumpProto.HotpotMainRoleTemp.newBuilder();
        builder.setGrid(parseGridBuilder(mainRole.getGrid()));
        builder.setLevel(mainRole.getLv());
        builder.setExp(mainRole.getExp());
        for (Map.Entry<eHotpotJumpAttr, Integer> entry : mainRole.getAttrMap().entrySet()) {
            HotpotJumpProto.HotpotMainRoleAttr.Builder attrBuilder = HotpotJumpProto.HotpotMainRoleAttr.newBuilder();
            attrBuilder.setType(entry.getKey().getValue());
            attrBuilder.setValue(entry.getValue());
            builder.addAttr(attrBuilder);
        }
        for (List<Integer> skill : mainRole.getSkills()) {
            HotpotJumpProto.HotpotMainRoleSkillTemp.Builder skillBuilder = HotpotJumpProto.HotpotMainRoleSkillTemp.newBuilder();
            skillBuilder.setId(skill.get(0));
            skillBuilder.setLevel(skill.get(1));
            builder.addSkill(skillBuilder);
        }

        return builder;
    }

    public static HotpotJumpProto.HotpotJumpGridTemp.Builder parseGridBuilder(HotpotJumpGrid grid) {
        HotpotJumpProto.HotpotJumpGridTemp.Builder builder = HotpotJumpProto.HotpotJumpGridTemp.newBuilder();
        builder.setX(grid.getX());
        builder.setY(grid.getY());
        if (grid.getHpMax() > 0) {
            builder.setHp(grid.getHp());
            builder.setHpMax(grid.getHpMax());
        }
        builder.setShield(grid.getShield());

        for (List<Integer> effect : grid.getEffects()) {
            int skillId = effect.get(0);
            int remainRound = effect.get(1);
            HotpotJumpProto.HotpotJumpGridEffectTemp.Builder effectBuilder = HotpotJumpProto.HotpotJumpGridEffectTemp.newBuilder();
            effectBuilder.setSkillId(skillId);
            effectBuilder.setRemainRound(remainRound);
            builder.addEffects(effectBuilder);
        }

        return builder;
    }

    public static List<HotpotJumpProto.HotpotJumpShowTemp> parseShowListBuilder(List<List<HotpotJumpShowGridChange>> showList) {
        List<HotpotJumpProto.HotpotJumpShowTemp> resultList = new ArrayList<>();
        for (List<HotpotJumpShowGridChange> shows : showList) {
            if (shows.isEmpty()) {
                continue;
            }
            HotpotJumpProto.HotpotJumpShowTemp.Builder builder = HotpotJumpProto.HotpotJumpShowTemp.newBuilder();
            for (HotpotJumpShowGridChange show : shows) {
                builder.addGridChange(parseGridChangeBuilder(show));
            }
            resultList.add(builder.build());

        }
        return resultList;
    }

    private static HotpotJumpProto.HotpotJumpGridChangeTemp parseGridChangeBuilder(HotpotJumpShowGridChange show) {
        HotpotJumpProto.HotpotJumpGridChangeTemp.Builder builder = HotpotJumpProto.HotpotJumpGridChangeTemp.newBuilder();
        builder.setChangeType(show.getShowType());
        if (show.getUnit() != null) {
            builder.setUnit(parseUnitBuilder(show.getUnit()));
        }
        for (HotpotJumpGrid grid : show.getGrids()) {
            builder.addGrid(parseGridBuilder(grid));
        }
        if (show.getMainRole() != null) {
            builder.setMainRole(parseMainRoleBuilder(show.getMainRole()));
        }
        if (show.getSkillId() > 0) {
            builder.setSkillId(show.getSkillId());
        }
        if (show.isCri()) {
            builder.setIsCritical(show.isCri());
        }
        if (show.getStep() > 0) {
            builder.setStep(show.getStep());
        }

        if (!show.getValues().isEmpty()) {
            builder.addAllValues(show.getValues());
        }
        return builder.build();
    }
}
